/******************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module: displayInstance.h
 * Author : Byungil Jeong
 *
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
 *****************************************************************************/
 
#ifndef DISPLAYINSTANCE_H_
#define DISPLAYINSTANCE_H_

#include "sageDisplay.h"
#include "sage.h"
#include "sageSync.h"
#include "sageCircBuf.h"
#include "sageTcpModule.h"
#include "sageUdpModule.h"
#include "streamInfo.h"

class gStreamRcv;
class displayInstance;

class streamData {
public:
	displayInstance *This;
	pthread_t       thId;
	int senderID;
	int idx;
	int queueID;
	sageMontage montage[2];
	int frontMon;
	bool active, renewMontage, configure, waitingSignal;
	bool updated;
	sageBlock dispBlock;
	
	int pixelBlockWidth, pixelBlockHeight;
    float bytesPerPixel;
	
	streamData() : active(false), renewMontage(false), frontMon(0), 
					updated(false), configure(false), waitingSignal(false) {}
	sageMontage* getFrontMon() { return &montage[frontMon]; }
	sageMontage* getBackMon() { return &montage[1-frontMon]; }
	int setDepth(float depth);
	int setTileID(int id);
	void swapMontage();
	int clearMontage();
	void storeBlockConfig();
};

class displayInstance {
private:
	gStreamRcv *rcv;
	streamProtocol *nwObj;
	
	int instID;
	int syncMode;
	int sailNodeNum;

	bool locked;
	bool dropflag;
	
	//sageCircBuf *blockBuf;
	
	std::vector<streamData*> streamList;
	sageSyncServer *syncServerObj;	
	sageSyncClient *syncClientObj;

	int streamNum;
	int frameNum;
	int masterFrameNum;
	int zValue;
	bool waitingSync;
	bool connectionDone;
	bool streamsUp;

	int oldFrame;
	int reportRate;
	
	double reportTime;
	unsigned long bandWidth;
	unsigned packetLoss;
	double oldTime;
	
	bool streamStop;
	bool noSync;
	bool unreliable;
	bool displayActive;  // indicate if it receives pixels or not
	
	pthread_mutex_t *condMutex;
	pthread_cond_t  *bufReady;
	pthread_cond_t  *beginWaiting;
	
	static void* syncMasterThread(void* args);
   static void* rcvThread(void* args);
	
	int sendPerformanceInfo();
	int createRcvThread(int senderID, int tileID);
	int initSyncConnection();
	int sendAckToSenders(int flag);
	int sendAckToSender(int senderID, int flag);
	int processSageBlock(int idx);
			
public:
	displayInstance() {}
	~displayInstance();
	int init(gStreamRcv *gsr, int id, streamProtocol *protoObj);
	int fetchSageBlocks();
	bool checkSyncSignal();
	int setDepth(float depth);
	int destroyInstance();
	int addStream(int senderID, int tileID);
	int rcvStream(int id);
	
	friend class gStreamRcv;
};

#endif
